//
//  SubMenu.m
//  CMOLTest
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SubMenu.h"
#import "UIView+Extension.h"
#import "DeviceMacro.h"
@interface SubMenu()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger height;
}
@property(nonatomic,assign)DirectionType type;
@property(nonatomic,weak)UIImageView *containView;
@property(nonatomic,strong)UITableView *table;
@end
@implementation SubMenu
-(instancetype)initWithDirection:(DirectionType)direction{
    self=[super init];
    if (self) {
        NSString *str;
        if (direction==DirectionRightType) {
            str=@"up_arrow";
        } if (direction==DirectionCenterType){
            str=@"pop_image";
        }
        UIImageView *bgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:str]];
        [self addSubview:bgView];
        self.containView=bgView;
        
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        view.backgroundColor=[UIColor grayColor];
        view.alpha=0.6;
        if (self.type==DirectionLeftType) {
            view.width=ScreenWidth-100;
            view.backgroundColor=[UIColor clearColor];
        }
        [self insertSubview:view belowSubview:self.containView];
        self.bgView=view;
        UITableView *table=[[UITableView alloc]initWithFrame:CGRectZero];
        table.backgroundColor=[UIColor clearColor];
        table.delegate=self;
        table.dataSource=self;
        table.bounces=NO;
        table.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self addSubview:table];
        self.table=table;
        
    }
    return self;
}
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    height=[self.datasource numberOfRowsInSubMenu];
        return height;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID=@"menu";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        if (self.type==DirectionLeftType) {
            cell.backgroundColor=[UIColor colorWithRed:80/255. green:80/255. blue:80/255. alpha:0.5];
            cell.textLabel.textColor=[UIColor whiteColor];
            cell.textLabel.font=[UIFont systemFontOfSize:16];
        }else{
        cell.backgroundColor=[UIColor clearColor];
        cell.textLabel.textColor=RGB(51, 51, 51);
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text=[self.datasource titileForSubMenuInRow:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self dismiss];
    if ([self.delegate respondsToSelector:@selector(SubMenuControllerDidClickIndex:)]) {
        [self.delegate SubMenuControllerDidClickIndex:indexPath.row];
    }
    
}
-(void)setController:(UIViewController *)controller{
    _controller=controller;
    self.content=controller.view;
}

-(void)setContent:(UIView *)content{
    _content=content;
    content.x=5;
    content.y=5;
    self.containView.width=CGRectGetMaxX(content.frame)+10;
    self.containView.height=CGRectGetMaxY(content.frame)+10;
    [self.containView addSubview:content];
}
-(void)showFrom:(UIView *)from{
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    [window addSubview:self];
    if (height>5) {
        height=5;
    }
    [self.table reloadData];
    self.frame= window.bounds;
    self.bgView.x=CGRectGetMaxX(from.frame);
    self.bgView.width=self.width-self.bgView.x;

    CGRect newFrame=[from convertRect:from.bounds toView:window];
    self.table.height=height*50;
    self.table.width=180;
    self.containView.width=170;
    self.containView.height=100;
    self.containView.x=CGRectGetMaxX(newFrame);
    self.containView.centerY = CGRectGetMidY(newFrame);
    self.table.x=CGRectGetMaxX(newFrame);
    self.table.y=self.containView.y;
    
}
-(void)showDownFrom:(UIView *)from withx:(CGFloat )x andWith:(CGFloat)width{
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    [window addSubview:self];
    [self.table reloadData];
    if (height>5) {
        height=5;
    }
    self.frame= window.bounds;
    CGRect newFrame=[from convertRect:from.bounds toView:window];
    self.table.height=height*50;
    self.table.width=width;
    self.containView.width=width;
    self.containView.height=self.table.height+10;
//    self.containView.centerX=CGRectGetMidX(newFrame);
//        self.containView.y = CGRectGetMaxY(newFrame)+10;
    if (self.type==DirectionLeftType) {
        self.containView.x=CGRectGetMinX(newFrame)-width-6;
        self.containView.centerY = newFrame.origin.y+newFrame.size.height/2.;
    }

    self.table.x=self.containView.x;
    self.table.y=self.containView.y+2;
    self.bgView.alpha=0.2;
    self.bgView.x=x;
 

    
    
}
-(void)showDownFrom:(UIView *)from withx:(CGFloat )x{
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    [window addSubview:self];
    [self.table reloadData];
    self.frame= window.bounds;
    CGRect newFrame=[from convertRect:from.bounds toView:window];
    if (height>5) {
        height=5;
    }
    self.table.height=height*44;
    self.table.width=180;
    self.containView.width=170;
    self.containView.height=self.table.height+15;
    self.containView.centerX=CGRectGetMidX(newFrame);
    self.containView.y = CGRectGetMaxY(newFrame)+8;
    self.table.x=self.containView.x;
    self.table.y=self.containView.y+10;
    self.bgView.alpha=0.2;
    self.bgView.x=x;
    
    
}

-(void)showDownFromOrder:(UIView *)from withx:(CGFloat )x{
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    [window addSubview:self];
    [self.table reloadData];
    self.frame= window.bounds;
    CGRect newFrame=[from convertRect:from.bounds toView:window];
    if (height>4) {
        height=4;
    }
    self.table.height=height*50;
    self.table.width=180;
    self.containView.width=170;
    self.containView.height=self.table.height+15;
    self.containView.centerX=CGRectGetMidX(newFrame);
    self.containView.y = CGRectGetMaxY(newFrame)+8;
    self.table.x=self.containView.x;
    self.table.y=self.containView.y+10;
    self.bgView.alpha=0.2;
    self.bgView.x=x;


}
- (void)dismiss
{
    [self removeFromSuperview];
}
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    if (CGRectContainsPoint(CGRectMake(ScreenWidth-60, 0, 100, ScreenHeight), point) ) {
        return NO;
    }
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}

@end
